package com.hzvictory.hotel_system.controller;

import com.hzvictory.hotel_system.common.constant.JwtClaimsConstant;
import com.hzvictory.hotel_system.common.core.controller.BaseController;
import com.hzvictory.hotel_system.common.properties.JwtProperties;
import com.hzvictory.hotel_system.common.result.Result;
import com.hzvictory.hotel_system.common.utils.JwtUtil;
import com.hzvictory.hotel_system.pojo.dto.ChangePasswordDTO;
import com.hzvictory.hotel_system.pojo.dto.UserLoginDTO;
import com.hzvictory.hotel_system.pojo.entity.User;
import com.hzvictory.hotel_system.pojo.vo.UserLoginVO;
import com.hzvictory.hotel_system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.hzvictory.hotel_system.common.annotation.Log;
import com.hzvictory.hotel_system.common.core.domain.AjaxResult;
import com.hzvictory.hotel_system.common.enums.BusinessType;
import com.hzvictory.hotel_system.common.core.page.TableDataInfo;
/**
 * 功能
 * 作者：何征
 * 日期：2025/7/14 21:23
 */
@RestController
@RequestMapping("auth")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("用户登录：{}", userLoginDTO);
        User user = userService.login(userLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(Long.valueOf(user.getId()))
                .userName(user.getUsername())
                .token(token)
                .isAdmin(user.getIsAdmin())
                .build();

        return Result.success(userLoginVO);
    }

    /**
     * 修改密码
     *
     * @param changePasswordDTO
     * @return
     */
    @PutMapping("/ChangePassword")
    @ApiOperation("修改密码")
    public AjaxResult ChangePassword(@RequestBody ChangePasswordDTO changePasswordDTO){
        log.info("修改密码：{}", changePasswordDTO);
        return toAjax(userService.ChangePassword(changePasswordDTO));
    }
    /**
     * 查询用户表列表
     */
    //@PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出用户表列表
     */
    //@PreAuthorize("@ss.hasPermi('system:user:export')")
//    @Log(title = "用户表", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, User user)
//    {
//        List<User> list = userService.selectUserList(user);
//        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
//        util.exportExcel(response, list, "用户表数据");
//    }

    /**
     * 获取用户表详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增用户表
     */
    //@PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "用户表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户表
     */
    //@PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户表
     */
    //@PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "用户表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }
}
